package com.amtf.datafile3.core.xsv.render;

import java.text.Format;

import com.amtf.datafile3.DataField;
import com.amtf.datafile3.DataFieldRender;
import com.amtf.datafile3.core.std.AbsDataFieldRender;

public class XsvDataFieldRender extends AbsDataFieldRender {
	protected char quote = ' ';
	protected boolean forceQuote = false;
	protected char delimiter = ',';

	public XsvDataFieldRender(char quote, char delimiter, boolean forceQuote) {
		this(null, quote, delimiter, forceQuote);
	}

	public XsvDataFieldRender(Format format, char quote, char delimiter, boolean forceQuote) {
		super(format);
		this.quote = quote;
		this.delimiter = delimiter;
		this.forceQuote = forceQuote;
	}

	public String rending(DataField value) {
		if (value.getValue() != null) {
			String tmpValue = null;
			if (format != null) {
				tmpValue = format.format(value.getValue());
			} else {
				tmpValue = value.getValue().toString();
			}

			if (quote != ' ') {
				if (forceQuote) {
					tmpValue = quote + tmpValue + quote;
				} else if (tmpValue.indexOf(delimiter) != -1) {
					tmpValue = quote + tmpValue + quote;
				}
			}

			return tmpValue;
		} else {
			return "";
		}
	}

	@Override
	public DataFieldRender clone() {
		XsvDataFieldRender newObj = new XsvDataFieldRender(format, quote, delimiter, forceQuote);
		return newObj;
	}

	public char getQuote() {
		return quote;
	}

	public void setQuote(char quote) {
		this.quote = quote;
	}

	public boolean isForceQuote() {
		return forceQuote;
	}

	public void setForceQuote(boolean forceQuote) {
		this.forceQuote = forceQuote;
	}

	public char getDelimiter() {
		return delimiter;
	}

	public void setDelimiter(char delimiter) {
		this.delimiter = delimiter;
	}

}
